package com.example.demo.controller;


import com.example.demo.domain.Grade;


import com.example.demo.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.text.NumberFormat;
import java.util.List;


@Controller
@RequestMapping("/pri/grade")
public class GradeController {
@Autowired
    private GradeService gradeService;
    //默认分页
    private static final int pageSize = 5;

    @Autowired
    public void setGradeService(GradeService gradeService){
        this.gradeService = gradeService;
    }

    /**
     * 查询分页查询成绩
     * /pri/grade/findAll?
     * @param model
     * @return
     */
    @GetMapping("/findAll")
    public String findAll(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                          Model model){
        Integer totalPage = null;
        pageNum = (pageNum-1)*pageSize;
        List<Grade> gradeList = gradeService.findAll(pageNum,pageSize);
        Integer pageTotal = gradeService.findTotalRecord();
        model.addAttribute("gradeList",gradeList);
        //实行分页
        if(pageTotal % pageSize == 0){
            totalPage = pageTotal / pageSize;
        }else{
            totalPage = pageTotal / pageSize + 1;
        }
        model.addAttribute("totalPage",totalPage);
        return "grade/list";
    }

    /**
     * 删除成绩信息
     * @param request
     * @return
     */
    @GetMapping("/delete")
    public String delete(HttpServletRequest request){

        int id = Integer.parseInt(request.getParameter("id"));
        String courseNo = request.getParameter("courseNo");
        gradeService.delete(id,courseNo);
        return "redirect:/pri/grade/findAll";
    }

    /**
     * 添加成绩信息
     * @param model
     * @return
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        model.addAttribute("grade", new Grade());
        return "grade/add";
    }

    @PostMapping("/add")
    public String add(Grade grade){
        gradeService.add(grade);
        return "redirect:/pri/grade/findAll";
    }

    /**
     * 修改成绩信息
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/toUpdate")
    public String toUpdate(HttpServletRequest request,Model model){
        int id = Integer.parseInt(request.getParameter("id"));
        String courseNo = request.getParameter("courseNo");
        Grade grade = gradeService.findGradeByIdAndNo(id,courseNo);
        model.addAttribute("grade", grade);
        return "grade/modify";
    }

    @PostMapping("/update")
    public String update(Grade grade){
        gradeService.update(grade);
        return "redirect:/pri/grade/findAll";
    }
    @GetMapping("/findNum")
    public String num(Model model,HttpServletRequest request){
//        Integer cid = Integer.parseInt(request.getParameter("cid"));
//       // int num = gradeService.findNumById(id);
//       // Data numDetail = dataService.findByCidDetail(cid);
//        dataService.
//       // Integer totalRecord = gradeService.findTotalRecord();
//        //int rate=num/totalRecord;
//
//        //model.addAttribute("numDetail",numDetail);
//        //model.addAttribute("num",num);
//        //model.addAttribute("rate",rate);
        Integer cid = Integer.parseInt(request.getParameter("cid"));
        Integer num = gradeService.findNumByCid(cid);
        Integer totalRecord = gradeService.findTotalRecordByCid(cid);


        NumberFormat numberFormat = NumberFormat.getInstance();
// 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String rate = numberFormat.format((float) num / (float) totalRecord * 100)+"%";
        model.addAttribute("num",num);
        model.addAttribute("cid",cid);
        model.addAttribute("rate",rate);

        return "data/detail";
    }



}
